package com.sgcc.operate.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.sgcc.common.core.domain.R;
import com.sgcc.common.security.service.TokenService;
import com.sgcc.operate.domain.OAttrField;
import com.sgcc.operate.mapper.OAttrFieldMapper;
import com.sgcc.system.api.RemoteDictDataService;
import com.sgcc.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.operate.mapper.ORuleAttrMapper;
import com.sgcc.operate.domain.ORuleAttr;
import com.sgcc.operate.service.IORuleAttrService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 人群规则属性Service业务层处理
 * 
 * @author sgcc
 * @date 2021-03-23
 */
@Service
public class ORuleAttrServiceImpl implements IORuleAttrService 
{
    @Autowired
    private ORuleAttrMapper oRuleAttrMapper;
    @Autowired
    private RemoteDictDataService remoteDictDataService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private OAttrFieldMapper oAttrFieldMapper;


    /**
     * 查询人群规则属性
     * 
     * @param id 人群规则属性ID
     * @return 人群规则属性
     */
    @Override
    public ORuleAttr selectORuleAttrById(String id)
    {
        ORuleAttr oRuleAttr = oRuleAttrMapper.selectORuleAttrById(id);
        OAttrField oAttrField = new OAttrField();
        oAttrField.setRuleattrid(oRuleAttr.getId());
        oRuleAttr.setAttrFieldList(oAttrFieldMapper.selectOAttrFieldList(oAttrField));
        return oRuleAttr;
    }

    /**
     * 查询人群规则属性列表
     * 
     * @param oRuleAttr 人群规则属性
     * @return 人群规则属性
     */
    @Override
    public List<ORuleAttr> selectORuleAttrList(ORuleAttr oRuleAttr)
    {
        List<ORuleAttr> oRuleAttrs = oRuleAttrMapper.selectORuleAttrList(oRuleAttr);
        for (ORuleAttr ruleAttr : oRuleAttrs) {
            OAttrField oAttrField = new OAttrField();
            oAttrField.setRuleattrid(oRuleAttr.getId());
            ruleAttr.setAttrFieldList(oAttrFieldMapper.selectOAttrFieldList(oAttrField));
        }

        return oRuleAttrs;
    }

    /**
     * 新增人群规则属性
     * 
     * @param oRuleAttr 人群规则属性
     * @return 结果
     */
    @Override
    @Transactional
    public int insertORuleAttr(ORuleAttr oRuleAttr)
    {
        LoginUser loginUser = tokenService.getLoginUser();
        oRuleAttr.setCreateDate(new Date());
        oRuleAttr.setUpdateDate(new Date());
        oRuleAttr.setCreateBy(loginUser.getUserid());
        oRuleAttr.setUpdateBy(loginUser.getUserid());
        oRuleAttr.setModifyorg(loginUser.getSysUser().getDeptId());
        oRuleAttr.setDelFlag("0");
        R idstr = remoteDictDataService.getIdStr("same_day_id_create", "rule_attr_id");
        if (idstr.getCode() == 200) {
            oRuleAttr.setId(idstr.getData()+"");
            oRuleAttrMapper.insertORuleAttr(oRuleAttr);
        } else {
            return 0;
        }


        for (OAttrField oAttrField : oRuleAttr.getAttrFieldList()){
            if (oAttrField.getId() == null){
//                continue;
            }
            if ("0".equals(oAttrField.getDelFlag())){
                if (StringUtils.isBlank(oAttrField.getId())){
                    String id= UUID.randomUUID().toString().replaceAll("-", "");
                    oAttrField.setId(id);
                    oAttrField.setRuleattrid(oRuleAttr.getId());
                    oAttrField.setUpdateBy(loginUser.getUserid());
                    oAttrField.setUpdateDate(new Date());
                    oAttrField.setCreateDate(new Date());
                    oAttrField.setCreateBy(loginUser.getUserid());
                    oAttrFieldMapper.insertOAttrField(oAttrField);
                }else{
                    oAttrField.setUpdateBy(loginUser.getUserid());
                    oAttrField.setUpdateDate(new Date());
                    oAttrFieldMapper.updateOAttrField(oAttrField);
                }
            }else{
                oAttrFieldMapper.deleteOAttrFieldById(oAttrField.getId());
            }
        }



        return 1;
    }

    /**
     * 修改人群规则属性
     * 
     * @param oRuleAttr 人群规则属性
     * @return 结果
     */
    @Override
    public int updateORuleAttr(ORuleAttr oRuleAttr)
    {
        LoginUser loginUser = tokenService.getLoginUser();
        oRuleAttr.setUpdateDate(new Date());
        oRuleAttr.setUpdateBy(loginUser.getUserid());
        oRuleAttrMapper.updateORuleAttr(oRuleAttr);

        for (OAttrField oAttrField : oRuleAttr.getAttrFieldList()){
            if (oAttrField.getId() == null){
//                continue;
            }
            if ("0".equals(oAttrField.getDelFlag())){
                if (StringUtils.isBlank(oAttrField.getId())){
                    String id= UUID.randomUUID().toString().replaceAll("-", "");
                    oAttrField.setId(id);
                    oAttrField.setRuleattrid(oRuleAttr.getId());
                    oAttrField.setUpdateBy(loginUser.getUserid());
                    oAttrField.setUpdateDate(new Date());
                    oAttrField.setCreateDate(new Date());
                    oAttrField.setCreateBy(loginUser.getUserid());
                    oAttrFieldMapper.insertOAttrField(oAttrField);
                }else{
                    oAttrField.setUpdateBy(loginUser.getUserid());
                    oAttrField.setUpdateDate(new Date());
                    oAttrFieldMapper.updateOAttrField(oAttrField);
                }
            }else{
                oAttrFieldMapper.deleteOAttrFieldById(oAttrField.getId());
            }
        }
        return 1;
    }

    /**
     * 批量删除人群规则属性
     * 
     * @param ids 需要删除的人群规则属性ID
     * @return 结果
     */
    @Override
    public int deleteORuleAttrByIds(String[] ids)
    {
        return oRuleAttrMapper.deleteORuleAttrByIds(ids);
    }

    /**
     * 删除人群规则属性信息
     * 
     * @param id 人群规则属性ID
     * @return 结果
     */
    @Override
    public int deleteORuleAttrById(String id)
    {
        return oRuleAttrMapper.deleteORuleAttrById(id);
    }

    @Override
    public boolean judgeQuoted(String ruleAttrId) {
        return oRuleAttrMapper.judgeQuoted(ruleAttrId);
    }
}
